package com.ncs.stm;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;
import com.ncs.stm.adapter.ImageAdapter;
import com.ncs.stm.entity.Category;
import com.ncs.stm.helper.HttpHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 *
 * Survey主界面
 *
 *
 */
public class SurveyTips extends Activity {

    private String userid;
    public static List<Map<String,Object>> categories;
    private List<Category> categoryList;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.survey);

        userid = getIntent().getStringExtra("userid");
        GridView gridView = (GridView)findViewById(R.id.survey_grid);
        Button cancelButton = (Button)findViewById(R.id.cancel);
        Button submitButton = (Button)findViewById(R.id.submit);
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(SurveyTips.this,MainActivity.class);
                startActivity(intent);

            }
        });

        submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(SurveyTips.this,Summary.class);
                intent.putExtra("userid",userid);
                startActivity(intent);
            }
        });

        SimpleAdapter imageAdapter = new SimpleAdapter(this,getSurveyCategories(),
                                                       R.layout.surveycategories,
                                                       new String[]{"surveyCatImage","catId","surveyCatDesc"},
                                                       new int []{R.id.surveyCatImage,R.id.catId,R.id.surveyCatDesc});
        gridView.setAdapter(imageAdapter);
        registerForContextMenu(gridView);

    }


    /**
     *
     * @return
     */
    @SuppressWarnings("unchecked")
    private List<Map<String,Object>> getSurveyCategories(){
        categories = new ArrayList<Map<String,Object>>();
        if(categoryList == null || categoryList.size() == 0){
            categoryList = HttpHelper.getCategoryListByGetDemo("http://192.168.82.163/STM/api/CoreValues/GetScheme/", Category.class);
        }

            int[] cats = new int[]{
                    R.drawable.img_1,R.drawable.img_2,R.drawable.img_3,
                    R.drawable.img_4,R.drawable.img_5,R.drawable.img_6,
                    R.drawable.img_7,R.drawable.img_8,R.drawable.img_9
            };



            String[] catDescs = new String[]{
                    getString(R.string.cat01,SelectedTips.getSelectTipsCount(userid,categoryList.get(0).getId()),3),getString(R.string.cat02,SelectedTips.getSelectTipsCount(userid,categoryList.get(1).getId()),3),getString(R.string.cat03,SelectedTips.getSelectTipsCount(userid,categoryList.get(2).getId()),3),
                    getString(R.string.cat04,SelectedTips.getSelectTipsCount(userid,categoryList.get(3).getId()),3),getString(R.string.cat05,SelectedTips.getSelectTipsCount(userid,categoryList.get(4).getId()),3),getString(R.string.cat06,SelectedTips.getSelectTipsCount(userid,categoryList.get(5).getId()),3),
                    getString(R.string.cat07,SelectedTips.getSelectTipsCount(userid,categoryList.get(6).getId()),3),getString(R.string.cat08,SelectedTips.getSelectTipsCount(userid,categoryList.get(7).getId()),3),getString(R.string.cat09,SelectedTips.getSelectTipsCount(userid,categoryList.get(8).getId()),3)
            };

            for(int i = 0; i < cats.length; i++){
                Map tempMap = new HashMap();
                tempMap.put("surveyCatImage",cats[i]);
                tempMap.put("catId",categoryList.get(i).getId());
                tempMap.put("surveyCatDesc",catDescs[i]);
                categories.add(tempMap);
            }
        return categories;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        getMenuInflater().inflate(R.menu.surveymenu,menu);
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();

        switch(item.getItemId()){
            case R.id.surveyMenuSelect : {
                Intent intent = new Intent(this,Tips.class);
                intent.putExtra("surveyResultId",((TextView)menuInfo.targetView.findViewById(R.id.catId)).getText().toString());
                intent.putExtra("userid",userid);
                startActivity(intent);
                break;
            }
            case R.id.surveyMenuModify : {
                Intent intent = new Intent(this,SelectedTips.class);
                intent.putExtra("surveyResultId",((TextView)menuInfo.targetView.findViewById(R.id.catId)).getText().toString());
                intent.putExtra("userid",userid);
                startActivity(intent);
                break;
            }

        }

        return true;
    }
}
